package com.swy.api;

import org.springframework.http.*;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.oauth2.client.DefaultOAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.AccessTokenRequest;
import org.springframework.security.oauth2.client.token.RequestEnhancer;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails;
import org.springframework.security.oauth2.common.AuthenticationScheme;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.*;

@RestController
public class LoginController {
    //资源API
    @PostMapping("/login")
    public ResponseEntity<Object> doLogin() {
        ResourceOwnerPasswordResourceDetails roprd = new ResourceOwnerPasswordResourceDetails();
        roprd.setPassword("123456");
        roprd.setUsername("user_1");
        roprd.setAccessTokenUri("http://localhost:8080/oauth/token");
        roprd.setClientId("client_1");
        roprd.setClientSecret("123456");
        roprd.setScope(Arrays.asList("select"));
        roprd.setAuthenticationScheme(AuthenticationScheme.form);
        roprd.setClientAuthenticationScheme(AuthenticationScheme.form);
        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(roprd,new DefaultOAuth2ClientContext());
        RequestEntity re = new RequestEntity(HttpMethod.GET,URI.create("http://www.baidu.com"));
        ResponseEntity<Object> r = restTemplate.exchange(re,Object.class);
        //System.out.println(restTemplate.getAccessToken());
        return ResponseEntity.ok(r.getBody());
    }

    public static void main(String[] agr){
        Map<String,Object> param = new HashMap<>();
        param.put("grant_type","password");
        param.put("username","user_1");
        param.put("password","123456");
        param.put("client_id","client_1");
        param.put("client_secret","123456");
        param.put("scope","select");
        //ClientCredentialsResourceDetails roprd = new ClientCredentialsResourceDetails();
        ResourceOwnerPasswordResourceDetails roprd = new ResourceOwnerPasswordResourceDetails();
        roprd.setPassword("123456");
        roprd.setUsername("user_1");
        roprd.setAccessTokenUri("http://127.0.0.1:8080/oauth/token");
        roprd.setClientId("client_1");
        roprd.setClientSecret("123456");
        roprd.setScope(Arrays.asList("select"));
        roprd.setAuthenticationScheme(AuthenticationScheme.form);
        roprd.setClientAuthenticationScheme(AuthenticationScheme.form);
        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(roprd,new DefaultOAuth2ClientContext());

        System.out.println(restTemplate.getAccessToken().getValue());
        //restTemplate.setAccessTokenProvider(new WeixinClientCredentialsAccessTokenProvider());
        //restTemplate.setRetryBadAccessTokens(true);
        /*restTemplate.getInterceptors().add(new ClientHttpRequestInterceptor() {

            public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws
                    IOException {
                request.getHeaders().setAcceptCharset(Arrays.asList(Charset.defaultCharset()));
                request.getHeaders().setAccept(Arrays.asList(MediaType.ALL));
                request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
                request.getHeaders().set("access_token","afadsasdfadsfasdfads");
                return execution.execute(request, body);
            }
        });*/
        /*for (HttpMessageConverter h:restTemplate.getMessageConverters()){
            if(h instanceof MappingJackson2HttpMessageConverter){
                ((MappingJackson2HttpMessageConverter)h).setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON_UTF8,MediaType.APPLICATION_XML));
            }
        }*/
        //HttpHeaders headers = new HttpHeaders();
        //headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        //headers.setAccept(Arrays.asList(MediaType.ALL));
        //RequestEntity re = new RequestEntity(HttpMethod.GET,URI.create("http://www.baidu.com"));
        //ResponseEntity<TokenResponse> r = restTemplate.exchange(re,TokenResponse.class);
        //re = new RequestEntity(new TokenRequest(),HttpMethod.POST,URI.create("http://127.0.0.1:8080/oauth/token"));

        //ResponseEntity<TokenResponse> r = new RestTemplate().exchange(re,TokenResponse.class);
        //System.out.println(r.getBody().getAccess_token());

    }
}

